package com.ling.luo.trade.utils;

import java.math.BigDecimal;

/**
 * 金额转换 工具类
 *
 * @author zqh
 */
public class AmountUtil {

	public  static final int TWO = 2;
	public  static final int THREE = 3;

	/**
	 * 金额分转元
	 * @param amount
	 * @return
	 */
	public static String fenToYuan(Long amount){
		if (null == amount) {
			return "0.00";
		}
		BigDecimal big=new BigDecimal(amount);
		return  big.divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_EVEN).toString();
	}

	/**
	 * 金额元转分
	 * @param amount
	 * @return
	 */
	public static Long yuanToFen(String amount) {
		int index = amount.indexOf(".");
		int length = amount.length();
		Long amLong = 0L;
		if (index == -1) {
			amLong = Long.valueOf(amount + "00");
		} else if (length - index >= THREE) {
			amLong = Long.valueOf((amount.substring(0, index + 3)).replace(".", ""));
		} else if (length - index == TWO) {
			amLong = Long.valueOf((amount.substring(0, index + 2)).replace(".", "") + "0");
		} else {
			amLong = Long.valueOf((amount.substring(0, index + 1)).replace(".", "") + "00");
		}
		return amLong;
	}

}
